<?php
include_once('admin_global.php');
bn_include_lib('bn_jquery');
$gf=new bn_genralfunc();
$post=new bn_post();
$theme->add_block('body');

$theme->add_script(jquery_element('#clear','click','
$("#title").val("");
$("#row_id").val("");
$(".editor").val("");
$("#cat_name a").css("background-color","");
$("$cat_name_id").val("");
'));

$theme->add_script(jquery_element('#submit','click','
var tit=$("#title").val();
var rid=$("#row_id").val();
var txt=$(".editor").val();
var cat_id=$("#cat_name_id").val();
$.post(window.location.href,"cmd=post&"+$("#page_div :input").serialize(),function(data){
alert(data); 
$("#row_id").val("");   
});'));

$row=new stdclass;
$cmd=$_POST['cmd'];
if(isset($cmd))
{
    extract($_POST);
   if($cmd=='post')
   {
    if($row_id=='')
    {
        
        $row_id=$post->insert($cat_name_id,$login->get_user_id(),$title,$editor,'disable','disable');
        $post->post_custom->set_post_thumbnil($row_id,$thumb);
        $post->post_custom->set_post_password($row_id,$username,$password);
        $post->post_custom->set_post_file($row_id,$file);
        $post->post_custom->set_post_custom($row_id,$name,$val);
        die($lang[77]);
    }
    else
    {
       $post->update($cat_name_id,$title,$editor,$login->get_user_id(),$row_id);
        $post->post_custom->set_post_thumbnil($row_id,$thumb);
        $post->post_custom->set_post_password($row_id,$username,$password);
        $post->post_custom->set_post_file($row_id,$file);
        $post->post_custom->set_post_custom($row_id,$name,$val);
       die($lang[76]);   
    }
   }
   
   if($cmd=='update')
   $row=$post->get_post($id)->fetchOBJ();
}

echo $post->post_custom->get_post_small_thumbnil($row->id);
?>
  <style scoped="true">
  label{
    border:1px solid gray;
    display: block;
    width: 98%;
    border-radius:5px 5px 0px 0px ;
    padding: 5px;
    background-color: silver;
    margin:5px 0 5px 0;
  }
  </style>
<div id="page_div">
<table>
<tr>
	<td><?php echo $lang[13] ?></td>
	<td>
    <?php
	$cat=new bn_category;
    echo $cat->treeview('cat_name',$row->cat_id);
    ?>
    </td>
</tr>
<tr>
    <td>
    <?= $lang[81]?>
    </td>
    <td>
    <br />
    <input type="hidden" name="thumb" id="thumb"/>
    <input type="submit" value="<?= $lang[82]?>" onclick="openWin('img','thumb')" class="button green"/>
    <br />
    <img id="thumbn" width="100" height="100" src="<?= $post->post_custom->get_post_small_thumbnil($row->id) ?>"/>
    <br />
    </td>
</tr>
<tr>
	<td><?php echo $lang[71] ?></td>
	<td><input type="text" size="100" autocomplete="on" name="title" id="title" class="input-text" value="<?= $row->title ?>"/></td>
</tr>
<tr>
	<td colspan="2"><?php echo $lang[72] ?><br />
	<?php $gf->editor($theme,$row->text); ?></td>
</tr>

<tr>
  <td colspan="2">
  <label><?= $lang[2] ?></label>
  <div class="accord">
<?php
$usps=$post->post_custom->get_post_password($row->id);	
?>
    <p><?= $lang[1] ?></p><input type="text" size="25" name="username" class="input-text" value="<?= $usps[0] ?>"/>
    <br />
    <p><?= $lang[2] ?></p><input type="text" size="25" name="password" class="input-text" value="<?= $usps[1] ?>"/>
  </div>
  </td>
</tr>

<tr>
  <td colspan="2">
  <label><?=  $lang[87] ?></label>
  <div class="accord">
  <br />
  <input type="submit" value="<?= $lang[87]?>" onclick="openWin('all','add_table')" class="button green"/>
  <br />
  <table id="files">
<?php
$files=$post->post_custom->post_file_table($row->id);
echo $files[0];	
?>
  </table>
  </div>
  </td>
</tr>

<tr>
  <td colspan="2">
  <label><?= $lang[90] ?></label>
  <div class="accord">
  <br />
  <table id="custom">
  <tr>
  <th><?= $lang[88] ?></th>
  <th><?= $lang[89] ?></th>
  <th><?= $lang[11] ?></th>
  </tr>
  <tbody>
<?php
$custom=$post->post_custom->post_custom_table($row->id);
echo $custom[0];	
?>
  </tbody>
  <tfoot>
      <tr>
          <td>
          <input type="text" size="25" id="name" class="input-text"/>
          </td>
          <td>
          <input type="text" size="25" id="value" class="input-text"/>
          </td>
          <td>
          <input type="submit" id="value_save" value="<?= $lang[15] ?>" class="button"/>
          </td>
      </tr>
  </tfoot>
  </table>
  </div>  
  </td>
</tr>
<tr>
	<td colspan="2">
    <br />
    <input type="submit" name="submit" id="submit" value="<?php echo $lang[15] ?>" class="button"/>
    <input type="submit" name="clear" id="clear" value="<?php echo $lang[17] ?>" class="button"/>
    <input type="hidden" name="row_id" id="row_id" value="<?= $row->id ?>"/>
    </td>
</tr>
</table>
</div>
    <script type="text/javascript">
    window.onload=function(){$('.accord').hide();}

    $('label').live('click',function(){
    $(this).next('div').toggle(700);
    }); 
    
    $('.delete').live('click',function(){
        if(confirm('<?= $lang[10] ?>'))
        $(this).closest('tr').remove();
    });
    var cnt_name=<?= $custom[1] ?>;
    $('#value_save').live('click',function(){
        var tr=$(this).closest('tr');
        var name=tr.find('#name');
        var val=tr.find('#value');
        var text="\n<tr>"+
        "\n<td><input type='text' size='25' name='name["+cnt_name+"]' value='"+name.val()+"' class='input-text'/></td>"+
        "\n<td><input type='text' size='25' name='val["+cnt_name+"]' value='"+val.val()+"' class='input-text'/><td>"+
        "\n<td><input type='submit' class='delete button red' value='<?= $lang[11] ?>'/><td>"+
        "\n</tr>";
        $("#custom").append(text);
        name.val('');
        val.val('');
        name.focus();
        cnt_name++;
    });
function openWin(type,func)
{
   myWindow=window.window.showModalDialog("upload.php?type="+type+"&func="+func,"name","dialogWidth:855px;dialogHeight:550px");
   myWindow.focus();
}
var cnt=<?= $files[1] ?>;
function add_table(text)
{
  text=text.split(',');
  var table='';
  
  for(var i=0;i<text.length;i++)
  {
    var file=text[i].split('|');
    table+="\n<tr>"; 
    table+="\n<td><input type='hidden' name='file["+cnt+"]' value='"+file[0]+"'/>"+file[1]+"</td>";
    table+="\n<td><input type='submit' class='delete button red' value='<?= $lang[11] ?>'/></td>";
    table+="\n</tr>"; 
    cnt++;
  } 

  $('#files').append(table);
}

function thumb(text)
{
    text=text.split('|');
    $('#thumb').val(text[0]);
    $('#thumbn').attr('src',text[0]);
}
    </script>
<?php
echo $cat->command_script;
$theme->render();
?>